/**
 * @file demo api for showModal
 * @author swan
 */

/* globals swan */

import {selComponent} from './utils/index';

Page({ // eslint-disable-line

    data: {
        num: 0,
        columnId: 0,
        page: 1,
        status: 1,
        text: '不超过18个字',
        list: [],
        count: 0,
        showPageStatus: true, // 是否显示页面状态
        loading: true, // 是否加载中
        loadingBtn: false, // 是否显示重新加载按钮
        loadingTitle: '网络不给力，请稍后重试', // 页面状态提示文案
        loadingIcon: 'content'// 页面状态图标 content/wifi
    },
    onLoad(options) {
        // this.previewRefresh();
        // console.log(options);
        this.firstFetchData(options.id, this.data.page);
    },
    onShow() {
        this.requestTask.then(requestData => {
            let res = this.getData('data');
            swan.setPageInfo({
                // title: res.title,
                title: '唯美资讯',
                keywords: '科技资讯,产品设计,移动互联网,运营文案',
                description: '唯美资讯提供科技资讯、产品设计、移动互联网、运营文案、等聚合信息。',
                articleTitle: '唯美资讯提供科技资讯、产品设计、移动互联网、运营文案！',
                success: res => {
                    // console.log('setPageInfo success');
                },
                fail: err => {
                    // console.log('setPageInfo fail', err);
                }
            });
        })
    },
    firstFetchData(id, page) {
        console.log(id);
        console.log(page);
        this.requestTask = new Promise((resolve, reject) => {
            const requestTask = swan.request({
                url: 'https://host.com/api/list?id='+ id + '&page='+ page,
                header: {
                    'content-type': 'application/json'
                },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                success: res => {
                    console.log(res);
                    this.setData({
                        columnId: res.data.columnId,
                        count: res.data.count,
                        list: res.data.data,
                        status: 1,
                        text: res.data.page ? `为你推荐${res.data.page}条更新` : '暂时没有更新，休息一下'
                    });
                    swan.nextTick(() => {
                        this.setData({
                            showPageStatus: false,
                            loaded: true
                        });
                    });
                },
                fail: err => {
                    swan.showToast({
                        title: '创建失败',
                        icon: 'none'
                    });
                    console.log('request fail', err);
                },
                complete: () => {
                    // console.log('request complete');
                }
            })
        });
    },

    fetchData(id, page) {
        console.log(id);
        console.log(page);
        this.requestTask = new Promise((resolve, reject) => {
            const requestTask = swan.request({
                url: 'https://host.com/api/list?id='+ id + '&page='+ page,
                header: {
                    'content-type': 'application/json'
                },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                success: res => {
                    var list = this.data.list;
                    console.log(list);
                    console.log(this.data.list);
                    this.setData({
                        columnId: res.data.columnId,
                        count: res.data.count,
                        list: list.concat(res.data.data),
                        status: 1,
                        text: res.data.page ? `为你推荐${res.data.page}条更新` : '暂时没有更新，休息一下'
                    });
                    swan.nextTick(() => {
                        this.setData({
                            showPageStatus: false,
                            loaded: true
                        });
                    });
                },
                fail: err => {
                    swan.showToast({
                        title: '创建失败',
                        icon: 'none'
                    });
                    console.log('request fail', err);
                },
                complete: () => {
                    // console.log('request complete');
                }
            })
        });
    },

    scrollToLower() {
        // console.log(this.data.page + 1);
        var page = this.data.page + 1;
        this.setData({
            status: 1,
            page: page
        });
        console.log(page);
        swan.nextTick(() => {
            this.fetchData(this.data.columnId, page);
        });
    },

    feedItemTap(e)
    {
        // swan.showModal({
        //     title: '请求到的数据',
        //     content: e.currentTarget.dataset.say,
        //     showCancel: false
        // });
        console.log(e);
        // swan.navigateTo({
        //     url: "/show?id="+e.currentTarget.dataset.say
        // });
    },
    clickArticle(e) {
        // swan.showModal({
        //     title: '请求到的数据',
        //     content: e.currentTarget.dataset.say,
        //     showCancel: false
        // });
        swan.navigateTo({
            url: "/show?id="+e.currentTarget.dataset.say
        });
    },
    reloadPage() {
        this.setData({
            loading: true,
            showPageStatus: true
        });
    },
});
